package com.xysq.activity;
/**
 * Copyright (c) 2015 XiaoYuanShenQi. All rights reserved.
 * <p/>
 * Author: Steward_Han
 * Date: 2015-6-23
 * Description: Add tab to mainActivity
 */

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TabHost;

import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.xysq.lemon.R;
import com.xysq.util.UserInfoKeeper;
import com.xysq.util.ViewFindUtils;
import com.xysq.widget.EditDialog;

@SuppressWarnings("deprecation")
public class MainTabActivity extends TabActivity implements OnCheckedChangeListener {

    public static final String TAG_INDEX = "index"; // 首页Tag
    public static final String TAG_WANT = "want"; // 我要Tag
    public static final String TAG_USER = "user"; // 个人信息Tag

    private TabHost tabHost;
    private Intent indexIntent;
    private Intent buyIntent;
    private Intent userIntent;
    private RelativeLayout tipsLayout;
    private RelativeLayout serviceLayout;
    private String index = TAG_INDEX;
    public final static String INDEX = "index";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);

        UmengUpdateAgent.setUpdateOnlyWifi(false);
        MobclickAgent.openActivityDurationTrack(false);
        UmengUpdateAgent.update(this);

        this.indexIntent = new Intent(this, IndexActivity.class);
        this.buyIntent = new Intent(this, WantBuyActivity.class);
        this.userIntent = new Intent(this, InfoActivity.class);

        ((RadioButton) findViewById(R.id.rbtn_tab_index)).setOnCheckedChangeListener(this);
        ((RadioButton) findViewById(R.id.rbtn_tab_want)).setOnCheckedChangeListener(this);
        ((RadioButton) findViewById(R.id.rbtn_tab_user)).setOnCheckedChangeListener(this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        super.onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setupIntent();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
                case R.id.rbtn_tab_index:
                    this.tabHost.setCurrentTabByTag(TAG_INDEX);
                    break;
                case R.id.rbtn_tab_want:
//                    this.tabHost.setCurrentTabByTag(TAG_WANT);
//                    break;XXx
                    showTalentDialog();
                    break;
                case R.id.rbtn_tab_user:
                    if (TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(this))) {
                        Intent intent = new Intent(MainTabActivity.this, LoginActivity.class);
                        intent.putExtra(LoginActivity.IS_MAIN, true);
                        startActivity(intent);
                    } else {
                        this.tabHost.setCurrentTabByTag(TAG_USER);
                    }
                    break;
            }
        }
    }

    private void showTalentDialog() {
        View inflate = View.inflate(MainTabActivity.this, R.layout.dialog_tips_and_service, null);
        final EditDialog editDialog = new EditDialog(MainTabActivity.this, inflate);
        tipsLayout = ViewFindUtils.find(inflate, R.id.layout_tips);
        serviceLayout = ViewFindUtils.find(inflate, R.id.layout_service);
        serviceLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO
            }
        });
        tipsLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(MainTabActivity.this))) {
                    Intent intent = new Intent(MainTabActivity.this, PublishGoodsActivity.class);
                    startActivity(intent);
                    editDialog.dismiss();
                }else {
                    startActivity(new Intent(MainTabActivity.this, LoginActivity.class));
                }
            }
        });
        editDialog.show();
    }

    private void setupIntent() {
        this.tabHost = getTabHost();
        TabHost localTabHost = this.tabHost;
        index = getIntent().getStringExtra(INDEX);
        if (TextUtils.isEmpty(index)) {
            index = TAG_INDEX;
        }
        if (index.equals(TAG_INDEX)) {
            ((RadioButton) findViewById(R.id.rbtn_tab_index)).setChecked(true);
            localTabHost.addTab(buildTabSpec(TAG_INDEX, R.string.tab_index,
                    R.drawable.ic_tab_index_selector, this.indexIntent));
            localTabHost.addTab(buildTabSpec(TAG_WANT, R.string.tab_want,
                    R.drawable.ic_tab_buy_selector, this.buyIntent));
            localTabHost.addTab(buildTabSpec(TAG_USER, R.string.tab_user,
                    R.drawable.ic_tab_user_selector, this.userIntent));
            this.tabHost.setCurrentTabByTag(TAG_INDEX);
        } else if (index.equals(TAG_WANT)) {
           ((RadioButton) findViewById(R.id.rbtn_tab_want)).setChecked(true);
          /*   localTabHost.addTab(buildTabSpec(TAG_WANT, R.string.tab_want,
                    R.drawable.ic_tab_buy_selector, this.buyIntent));
            localTabHost.addTab(buildTabSpec(TAG_INDEX, R.string.tab_index,
                    R.drawable.ic_tab_index_selector, this.indexIntent));
            localTabHost.addTab(buildTabSpec(TAG_USER, R.string.tab_user,
                    R.drawable.ic_tab_user_selector, this.userIntent));*/
            this.tabHost.setCurrentTabByTag(TAG_WANT);
        } else if (index.equals(TAG_USER)) {
            ((RadioButton) findViewById(R.id.rbtn_tab_user)).setChecked(true);
/*            ((RadioButton) findViewById(R.id.rbtn_tab_user)).performClick();
            localTabHost.addTab(buildTabSpec(TAG_USER, R.string.tab_user,
                    R.drawable.ic_tab_user_selector, this.userIntent));
            localTabHost.addTab(buildTabSpec(TAG_INDEX, R.string.tab_index,
                    R.drawable.ic_tab_index_selector, this.indexIntent));
            localTabHost.addTab(buildTabSpec(TAG_WANT, R.string.tab_want,
                    R.drawable.ic_tab_buy_selector, this.buyIntent));*/
            this.tabHost.setCurrentTabByTag(TAG_USER);

        }

    }

    private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
                                         final Intent content) {
        return this.tabHost.newTabSpec(tag).setIndicator(getString(resLabel),
                getResources().getDrawable(resIcon)).setContent(content);
    }
}